package com.xiaoma.myaudience.biz.activity;

import java.util.ArrayList;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;

import com.xiaoma.myaudience.R;
import com.xiaoma.myaudience.biz.model.CategoryOptionInfo;
import com.xiaoma.myaudience.biz.task.CategoryOptionInfoTask;
import com.xiaoma.myaudience.biz.task.DataAsyncTask;
import com.xiaoma.myaudience.biz.util.VideoPrefHelper;
import com.xiaoma.myaudience.util.activity.BaseTabActivity;

/**
 * 排行榜页面.
 * 
 */
public class RankingActivity extends BaseTabActivity {
	public static Context mContext;

	private static final int MENU_REFRESH = Menu.FIRST;
	
	public static final String TAG = "CategoryActivity";
	// 按评分
	public static final String SCORE_SPEC = "score";
	// 按参加人数
	public static final String NUM_SPEC = "num";
	// 按查看次数
	public static final String TIME_SPEC = "time";
	
	public static final String OPTION_TAG = "_option";
	public static final String FILM_OPTION = "film_option";
	public static final String TV_OPTION = "tv_option";
	public static final String DOC_OPTION = "doc_option";
	
	private static final int HEAD_TAB = 1;
	private static final int MID_TAB = HEAD_TAB + 1;
	private static final int TAIL_TAB = MID_TAB + 1;

	private TabHost mTabHost;
	private CategoryOptionInfoTask mOptionInfoTask;
	private ArrayList<CategoryOptionInfo> mCategoryOptionData;
	private View mCurrentContentView;
    private int mCurrentTab = -1;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.category_activity);

		mContext = this;
		((ImageView) findViewById(R.id.base_title_bar_icon)).setImageResource(R.drawable.home_title_icon);
		((TextView)findViewById(R.id.base_title_bar_title)).setText(R.string.record_tab);
		setupTabs();
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		
	}
	
	@Override
	protected void onRestart() {
		// TODO Auto-generated method stub
		super.onRestart();
	}
	/**
	 * 返回context
	 * @return
	 */
	public Context getContext() {
		return mContext;
	}

	/**
	 * TabHost 初始化
	 */
	private void setupTabs() {
		mTabHost = getTabHost();
		mTabHost.setOnTabChangedListener(this);
		mTabHost.setup();

		addTab(SCORE_SPEC, R.string.category_film, HEAD_TAB);
		addTab(NUM_SPEC, R.string.category_tv, MID_TAB);
		addTab(TIME_SPEC, R.string.category_doc, TAIL_TAB);

		mTabHost.setCurrentTab(0);
	}
	
	/**
	 * 添加tab
	 * @param spec 
	 * @param titleid
	 * @param type
	 */
	private void addTab(String spec, int titleid, int type) {
		Intent intent = new Intent();
		intent.putExtra("tab_index", type);
		intent.setClass(this, RankingListActivity.class);
		intent.putExtra(TAG, spec);
		TabHost.TabSpec localTabSpec = mTabHost.newTabSpec(spec);
		localTabSpec.setIndicator(getTabIndicator(titleid, type));
		localTabSpec.setContent(intent);
		mTabHost.addTab(localTabSpec);
	}

    /**
     * 生成tab indicator
     * @param resourceid 图片资源id
     * @param type tab位置处于开始，中间还是末尾，不同的位置放置不同的图片
     * @return
     */
	private View getTabIndicator(int resourceid, int type) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.category_tabwidget_layout, null);
        TextView button = (TextView) view.findViewById(R.id.category_tab);     
        button.setText(resourceid);
		switch(type) {
		case HEAD_TAB:
			button.setBackgroundResource(R.drawable.category_head_tab_indicator);
			break;
		case MID_TAB:
			button.setBackgroundResource(R.drawable.category_head_tab_indicator);
			break;
		case TAIL_TAB:
			button.setBackgroundResource(R.drawable.category_head_tab_indicator);
			break;
		}
		
		return view;
	}


	@Override
    public void onTabChanged(String tabId) {
        TabHost tabHost = getTabHost();

        Animation enterAnimation = null;
        Animation exitAnimation = null;

        final int last = mCurrentTab;
        final int now = tabHost.getCurrentTab();

        if (last != -1) {
            if (last > now) {
                enterAnimation = AnimationUtils.loadAnimation(this, R.anim.tab_enter_from_left);
                exitAnimation = AnimationUtils.loadAnimation(this, R.anim.tab_exit_from_left);
            } else if (last < now) {
                enterAnimation = AnimationUtils.loadAnimation(this, R.anim.tab_enter_from_right);
                exitAnimation = AnimationUtils.loadAnimation(this, R.anim.tab_exit_from_right);
            }
        }

        if (mCurrentContentView != null && exitAnimation != null) {
            mCurrentContentView.setAnimation(exitAnimation);
        }

        mCurrentContentView = tabHost.getCurrentView();
        mCurrentTab = now;

        if (enterAnimation != null) {
            mCurrentContentView.setAnimation(enterAnimation);
        }
        
    }

	/**
	 * 取消数据的获取
	 */
    protected void cancelOptionInfoLoadingTask() {
		// TODO Auto-generated method stub
		if (mOptionInfoTask != null) {
			mOptionInfoTask.cancel(true, true);
		}
		mOptionInfoTask = null;
	}
	
	/**
	 * 获取筛选框里的数据
	 * @param category
	 */
	private void startOptionInfoTask(String category) {
		cancelOptionInfoLoadingTask();
		mOptionInfoTask = new CategoryOptionInfoTask(mContext, DataAsyncTask.TASK_CATEGORY_OPTION, this);
		mOptionInfoTask.execute(category);
	}
	
	@Override
	public void onPreExecute(int what) {
		// TODO Auto-generated method stub
		super.onPreExecute(what);
		switch(what) {
		case DataAsyncTask.TASK_CATEGORY_OPTION:
			
			break;
		}
	}

	@Override
	public void onPostExecute(int what, Object o) {
		// TODO Auto-generated method stub
		super.onPostExecute(what, o);

		switch (what) {
		case DataAsyncTask.TASK_CATEGORY_OPTION:
			if (o == null) {
				// TODO 有加载错误...
				return;
			}
			
			final String tabTag = (String)o;
            JSONObject jsonObj = null;;
            try {
                jsonObj = new JSONObject(VideoPrefHelper.getString(this, tabTag + OPTION_TAG,
                        "{}"));
            } catch (JSONException e) {
                e.printStackTrace();
            }
        	
        	mCategoryOptionData = CategoryOptionInfoTask.jsonObject2CategoryOptionInfoList(jsonObj);
			break;

		default:
			break;
		}

	}
	
	

	@Override
	protected void onDestroy() {
		super.onDestroy();
		cancelOptionInfoLoadingTask();
	}

}
